package com.atguigu.executor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@EnableConfigurationProperties(ThreadPoolProperties.class)
@Configuration
public class ThreadPoolConfig {

    @Autowired
    private ThreadPoolProperties myProperties;

    @Bean
    public ThreadPoolExecutor poolExecutor() {
        /**
         *  高并发场景下 150线程进来
         *   16个线程会立即得到处理
         *   拓展16个线程继续处理
         *   100个进入到队列当中
         *   剩余的18个会才去拒绝策略
         *  采用哪种拒绝策略
         *   LinkedBlockingQueue
         *      链表不会出现内存不连续问题
         *   ArrayBlockingQueue
         *      空间碎片问题, 导致内存空间不联系
         */
        return new ThreadPoolExecutor(myProperties.getCorePoolSize(),
                myProperties.getMaximumPoolSize(),
                myProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(myProperties.getQueueLength()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
